package com.mycat.dfs.util;

import com.mycat.dfs.test.WaterMarkUtils;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;


/**
 * 文件操作类型 Created by Administrator on 2016/8/25.
 *
 * @author Roger
 */
public class FileOperation {

    /**
     * 图片所有类型
     */
    private static List<String> imageType = new ArrayList<>();

    static {
        imageType.add("bmp");
        imageType.add("gif");
        imageType.add("jpg");
        imageType.add("pic");
        imageType.add("png");
        imageType.add("tif");
    }

    /**
     * 判断当前扩展名是否为图片
     *
     * @param extName 文件后缀名
     * @return 为图片时返回true
     */
    public static boolean isPicture(String extName) {
        if (null == extName || "".equals(extName)) {
            return false;
        }
        return imageType.contains(extName.toLowerCase());
    }

    /**
     * 压缩图片
     *
     * @param originPicture 压缩前图片流
     * @param compressWidth 压缩后的宽
     * @param compressHight 压缩后的高
     * @param extName       图片后缀名
     * @return 压缩后的图片流
     */
    public static InputStream fixPicture(InputStream originPicture, int compressWidth, int compressHight, String extName) {
        InputStream picture = null;
        BufferedImage tmpImage;
        try {
            BufferedImage image = Thumbnails.of(originPicture).scale(1).asBufferedImage();
            int width = image.getWidth();
            int height = image.getHeight();
            if (width > height) {
                tmpImage = Thumbnails.of(image).sourceRegion(Positions.CENTER, height, height)
                        .size(compressWidth, compressHight).keepAspectRatio(false).asBufferedImage();
            } else {
                tmpImage = Thumbnails.of(image).sourceRegion(Positions.TOP_LEFT, width, width)
                        .size(compressWidth, compressHight).keepAspectRatio(false).asBufferedImage();
            }
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(tmpImage, extName, os);
            picture = new ByteArrayInputStream(os.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return picture;
    }

    /**
     * 将byte[]转换为图片
     *
     * @param data byte[]
     * @param path 图片地址,需要包换图片名
     */
    public static void byteToImage(byte[] data, String path) {
        if (data.length < 3 || path.equals(""))
            return;
        try {
            FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
            imageOutput.write(data, 0, data.length);
            imageOutput.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 图片添加水印
     *
     * @param backGroupImage 需要添加水印的图片流
     * @param extName        图片扩展名
     * @param address        打卡地址
     * @param date           打卡时间
     */
    public static InputStream mark(InputStream backGroupImage, String extName, String address, String date) {
        InputStream picture = null;
        try {
            Image srcImg = ImageIO.read(backGroupImage);
            int srcImgWidth = srcImg.getWidth(null);
            int srcImgHeight = srcImg.getHeight(null);
            // 加水印
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufImg.createGraphics();
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            Font font = new Font("微软雅黑", Font.PLAIN, 28);
            g.setColor(Color.white); // 设置水印文字的颜色
            g.setFont(font);

            // 画地址
            int x = srcImgWidth - getWatermarkLength(address, g) - 3;
            int y = srcImgHeight - 3 - 32;
            g.drawString(address, x, y);
            // 画时间
            x = srcImgWidth - getWatermarkLength(date, g) - 3;
            y = y + 32;
            g.drawString(date, x, y);

            g.dispose();
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(bufImg, extName, os);
            picture = new ByteArrayInputStream(os.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return picture;
    }

    /**
     * 获取水印文字总长度
     *
     * @param waterMarkContent 水印的文字
     * @param g
     * @return 水印文字总长度
     */
    public static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
        return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
    }

    public static void main(String[] args) {
        // 原图位置, 输出图片位置, 水印文字颜色, 水印文字
        try {
            InputStream inputStream = mark(new FileInputStream(new File("d:\\att.jpg")), "jpg", "高新区天府五街G5", "2017-07-01 14:35:00");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }


}
